package helloworld.web.administrate;

import helloworld.entity.administrate.Role;
import helloworld.modules.hibernate.PropertyFilter;
import helloworld.modules.struts2.CrudActionSupport;
import helloworld.modules.struts2.Struts2Utils;
import helloworld.service.administrate.UserService;

import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

@Namespace("/administrate")
@Results( { @Result(name = CrudActionSupport.RELOAD, location = "role.action", type = "redirect") })
public class RoleAction extends CrudActionSupport<Role> {

	private static final long serialVersionUID = -5157079612828617166L;
	
	private UserService userService;
	
	private Long id;
	private Role entity;
	private List<Role> roleList;
	
	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String input() throws Exception {
		
		return INPUT;
	}

	@Override
	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		roleList = userService.searchRole(filters);
		return SUCCESS;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (id != null) {
			entity = userService.getRole(id);
		} else {
			entity = new Role();
		}
	}

	@Override
	public String save() throws Exception {
		userService.saveRole(entity);
		addActionMessage("保存角色["+entity.getName()+"]成功");
		return RELOAD;
	}

	public Role getModel() {
		return entity;
	}

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Role getEntity() {
		return entity;
	}

	public void setEntity(Role entity) {
		this.entity = entity;
	}

	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	
}
